/* File:        	PDPConfig.java
* Package:			org.esi.authz.pdp.config
* Created:      	29-abr-2011
* Last Changed: 	0:08:09
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.pdp.config;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

import com.tecnalia.authz.common.Resource;

// TODO: Auto-generated Javadoc
/**
 * The Class PDPConfig.
 */
public class PDPConfig {

	/** The default cfg file name. */
	private String defaultCfgFileName = "pdpconfig.properties";
	
	/** The domain name. */
	private String domainName;
	
	/** The multi policies combine alg. */
	private String multiPoliciesCombineAlg;
	
	/** The support must be present. */
	private String supportMustBePresent;
	
	/** The primary policy configuration. */
	private String primaryPolicyConfiguration;
   
	/** The Daemon port. */
	private int DaemonPort;
	
	/** The Daemon host. */
	private String DaemonHost;

	/** The Mgm port. */
	private int MgmPort;
	
	/** The Mgm host. */
	private String MgmHost;
	
	/** The Valid pep. */
	public Hashtable<String, Integer> ValidPEP =new Hashtable<String, Integer>();
   
	/**
	 * Instantiates a new pDP config.
	 *
	 * @param fileName the file name
	 */
	public PDPConfig(String fileName) {
		this.loadConfig(fileName);
	}
	
	/**
	 * Instantiates a new pDP config.
	 */
	public PDPConfig() {
		this.loadConfig(defaultCfgFileName);		
	}


	/**
	 * Gets the domain name.
	 *
	 * @return the domain name
	 */
	public String getDomainName() {
		return domainName;
	}

	/**
	 * Sets the domain name.
	 *
	 * @param domainName the new domain name
	 */
	public void setDomainName(String domainName) {
		this.domainName = domainName;
	}

	/**
	 * Gets the multi policies combine alg.
	 *
	 * @return the multi policies combine alg
	 */
	public String getMultiPoliciesCombineAlg() {
		return multiPoliciesCombineAlg;
	}

	/**
	 * Sets the multi policies combine alg.
	 *
	 * @param multiPoliciesCombineAlg the new multi policies combine alg
	 */
	public void setMultiPoliciesCombineAlg(String multiPoliciesCombineAlg) {
		this.multiPoliciesCombineAlg = multiPoliciesCombineAlg;
	}

	/**
	 * Gets the support must be present.
	 *
	 * @return the support must be present
	 */
	public String getSupportMustBePresent() {
		return supportMustBePresent;
	}

	/**
	 * Sets the support must be present.
	 *
	 * @param supportMustBePresent the new support must be present
	 */
	public void setSupportMustBePresent(String supportMustBePresent) {
		this.supportMustBePresent = supportMustBePresent;
	}

	/**
	 * Gets the primary policy configuration.
	 *
	 * @return the primary policy configuration
	 */
	public String getPrimaryPolicyConfiguration() {
		return primaryPolicyConfiguration;
	}

	/**
	 * Sets the primary policy configuration.
	 *
	 * @param primaryPolicyConfiguration the new primary policy configuration
	 */
	public void setPrimaryPolicyConfiguration(String primaryPolicyConfiguration) {
		this.primaryPolicyConfiguration = primaryPolicyConfiguration;
	}	
	
	/**
	 * Sets the mgm port.
	 *
	 * @param mgmPort the new mgm port
	 */
	public void setMgmPort(int mgmPort) {
		MgmPort = mgmPort;
	}

	/**
	 * Gets the mgm host.
	 *
	 * @return the mgm host
	 */
	public String getMgmHost() {
		return MgmHost;
	}

	/**
	 * Sets the mgm host.
	 *
	 * @param mgmHost the new mgm host
	 */
	public void setMgmHost(String mgmHost) {
		MgmHost = mgmHost;
	}

	/**
	 * Gets the mgm port.
	 *
	 * @return the mgm port
	 */
	public int getMgmPort() {
		return MgmPort;
	}

	/**
	 * Gets the daemon port.
	 *
	 * @return the daemon port
	 */
	public int getDaemonPort() {
		return DaemonPort;
	}

	/**
	 * Sets the daemon port.
	 *
	 * @param daemonPort the new daemon port
	 */
	public void setDaemonPort(int daemonPort) {
		DaemonPort = daemonPort;
	}

	/**
	 * Gets the daemon host.
	 *
	 * @return the daemon host
	 */
	public String getDaemonHost() {
		return DaemonHost;
	}

	/**
	 * Sets the daemon host.
	 *
	 * @param daemonHost the new daemon host
	 */
	public void setDaemonHost(String daemonHost) {
		DaemonHost = daemonHost;
	}


	/**
	 * Load config.
	 *
	 * @param fileName the file name
	 */
	public void loadConfig(String fileName){		
		
		Resource res = new Resource();
		Properties config = new Properties();
		try {
			config.load(res.getResourceAsStream(fileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setDomainName(config.getProperty("domainName"));			
		this.setMultiPoliciesCombineAlg(config.getProperty("multiPoliciesCombineAlg"));			
		this.setPrimaryPolicyConfiguration(config.getProperty("primaryPolicyConfiguration"));			
		this.setSupportMustBePresent(config.getProperty("supportMustBePresent"));
		this.setDaemonPort(Integer.parseInt(config.getProperty("DaemonPort")));
		this.setDaemonHost(config.getProperty("DaemonHost"));
		this.setMgmPort(Integer.parseInt(config.getProperty("MgmPort")));
		this.setMgmHost(config.getProperty("MgmHost"));
		
		//Retreive the list of valid PEP
		Enumeration<?> e = config.propertyNames();

	    while (e.hasMoreElements()) {
	      String key = (String) e.nextElement();
	      if(key.contains("PEP.")){
		      String listPEP = key.substring(4);
	    	  ValidPEP.put(listPEP, Integer.parseInt(config.getProperty(key)));
	      }
	    }
	}
	
	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		PDPConfig pdpconfig = new PDPConfig();
	}
}
